import { message } from 'antd';
import axios, { AxiosResponse } from 'axios';
import { baseURL } from '../config/configFile';
import Article from '../types/Article';
import Response from '../types/Response';

// axios实例
const axiosInstance = axios.create({
    baseURL
})

//请求所有文章
export async function queryAllArticles(): Promise<Array<Article>> {
    try {
        const { data }: AxiosResponse<Response<Array<Article>>> = await axiosInstance.get('/queryAllArticles');
        if (!!data.success) {
            return data.data;
        } else {
            message.warning(data.message);
            return [];
        }
    }
    catch (e) {
        message.error('网络错误');
        return [];
    }
}

//根据ID请求文章
export async function queryArticlesOnID(articleID: number): Promise<Article | null> {
    try {
        const { data }: AxiosResponse<Response<Article>> = await axiosInstance.get('/queryArticleOnID',
            { params: { articleID } });
        if (!!data.success) {
            return data.data;
        } else {
            message.warning(data.message);
            return null;
        }
    }
    catch (e) {
        message.error('网络错误');
        return null;
    }
}

//根据类别请求文章
export async function queryArticlesOnCate(cateID: number): Promise<Array<Article> | null> {
    try {
        const { data }: AxiosResponse<Response<Array<Article>>> = await axiosInstance.get('/queryArticlesOnCate',
            { params: { cateID: cateID } });
        if (!!data.success) {
            return data.data;
        } else {
            message.warning(data.message);
            return null;
        }
    }
    catch (e) {
        message.error('网络错误');
        return null;
    }
}

//新增文章
export async function addArticle(title: string, markdown: string, cateID: number): Promise<boolean> {
    try {
        const { data }: AxiosResponse<Response<boolean>> = await axiosInstance.post('/addArticle',
            { title, markdown, cateID });
        if (!!data.success) {
            return true;
        } else {
            message.warning(data.message);
            return false;
        }
    }
    catch (e) {
        message.error('网络错误');
        return false;
    }
}

//编辑文章
export async function editArticle(title: string, markdown: string, cateID: number, articleID: number): Promise<boolean> {
    try {
        const { data }: AxiosResponse<Response<boolean>> = await axiosInstance.post('/editArticle',
            { title, markdown, cateID, articleID });
        if (!!data.success) {
            return true;
        } else {
            message.warning(data.message);
            return false;
        }
    }
    catch (e) {
        message.error('网络错误');
        return false;
    }
}

//删除文章
export async function deleteArticle(articleID: number): Promise<boolean> {
    try {
        const { data }: AxiosResponse<Response<boolean>> = await axiosInstance.get('/deleteArticle',
            { params: { articleID } });
        if (!!data.success) {
            return true;
        } else {
            message.warning(data.message);
            return false;
        }
    }
    catch (e) {
        message.error('网络错误');
        return false;
    }
}